using Csla;
using System;
using System.Data;
using System.Collections;
using System.Data.Common;
using System.Data.SqlClient;
using Csla.Data;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;

namespace BO
{
    [Serializable()]
    public class SQLExcuteAdapter : Csla.CommandBase<SQLExcuteAdapter>
	{
		#region " Business Properties and Methods "

        int _RowCount= 0;
        public int RowCount
        {
            get { return _RowCount; }
            set { _RowCount = value; }
        }

        private int GetBinDataTable()
        {
            return 0;
        }

     
		#endregion
		
		#region " Static Methods "

        public static int Execute(string pSqlStr, CommandType pCommandType= CommandType.Text, params  SqlParameter[] parameters)
        {
            SQLExcuteAdapter adapter = ((SQLExcuteAdapter)DataPortal.Fetch<SQLExcuteAdapter>(new Criteria(pCommandType, pSqlStr, parameters)));
            //DataPortal.Execute<SQLExcuteAdapter>(new Criteria(pCommandType, pSqlStr, parameters));
            return 0;

        }
	
		#endregion
		
		#region " Criteria "
		
		// criteria for identifying existing object
		[Serializable()]
        public class Criteria
		{
			public string SqlStr;
            public CommandType CommandType;
            public SqlParameter[] Parameters { get; set; }

            public Criteria(CommandType pCommandType, string pSqlStr, params  SqlParameter[] parameters)
            {
                this.SqlStr = pSqlStr;
                this.CommandType = pCommandType;
                this.Parameters = parameters;
			}
			
		}
		
		#endregion
		
		#region " Data Access "
		
		protected void DataPortal_Fetch(object criteria)
		{


            Criteria crit = (Criteria)criteria;


            using (DbConnection cn = DbFactory.GetConnection(Database.CompanyConnection))
            {
              //  cn.Open();


                using (DbCommand cm = cn.CreateCommand())
                {
                    cm.CommandTimeout = 0;
                    cm.CommandType = crit.CommandType;
                    cm.CommandText = crit.SqlStr;

                    if (crit.Parameters != null)
                    {
                        foreach (SqlParameter param in crit.Parameters)
                        {
                            cm.Parameters.Add(param);
                        }
                    }

                    DataServeice.BatchUpdateData(new System.Collections.Generic.List<DbCommand>() { cm });
                     


                }
            }
			
		}

       
    

		#endregion
		
	}
	
}
